﻿#ifndef HEADER_E03C655FED70AE56
#define HEADER_E03C655FED70AE56

//录像索引检索类
#ifndef _REC_FRAME_INDEX_H_
#define _REC_FRAME_INDEX_H_
#include "rec_info.h"
#include "cross_com.h"
//混合索引
class CRecFrameIdx
{
public:
	CRecFrameIdx();
	~CRecFrameIdx();
public:
	int     Open(uint32_t bufSize,uint32_t indexOffset,uint32_t diskSecSize);
	void    InitStartPos(uint32_t dataPos,uint32_t idxPos);
	void    Reset();
	//写入视频帧索引
	int     WriteFrameIndex(const void * idxBuf,uint32_t idxSize);

	//写入GPS信息
	int     WriteGpsInfo(const void * gpsBuf,uint32_t gpsInfoSize);

	int     MoveData();
	//判断是否需要刷新
	bool    IsNeedFlush();
	int     AnalyseData(uint32_t headDiff = 0);
	int     GetFrmIdxData(STFS_REC_IDX * idxBuf,int maxNum);
	int     GetGpsIdxData(STFS_GPS_IDX * idxBuf,int maxNum);
	//录像时使用
	//获取要写入的偏移
	uchar_t * GetWriteBuffAddr(uint32_t & writePos,uint32_t & wtSize);
	uint32_t  GetWriteOffset();
  	uint32_t  GetDataSize2KeyFrm(int lastIdxCnt,uint32_t lastOffset);

	void UpdataLastWritePos()
	{
		m_lastWritePos = m_dataPos;
	}

	uint32_t GetBufSize()
	{
		return m_bufSize;
	}

	uint32_t GetDataSize()
	{
		return m_dataPos - m_lastWritePos;
	}

	uchar_t *  GetBufAddr()
	{
		return m_buf;
	}

	uint32_t  GetFrmIdxNum()
	{
		return m_frmIdxCnt;
	}

	uint32_t  GetGPSInfoNum()
	{
		return m_gpsIdxCnt;
	}
	
	void SetDelayWrite(uint32_t delayWtSecs)
	{
		m_delayWtSecs = delayWtSecs;
	}

	uint32_t GetLastFrmTime()
	{
		return m_lastFrmTm;
	}

	uint32_t GetLastFrmOffset()
	{
		return m_lastFrmOffset;  //最后一个I帧的偏移
	}

	uint32_t GetLastFrmLen()	//最后一个I帧的长度
	{
		return m_lastFrmLen;
	}

	uint32_t GetDataPos()
	{
		return m_dataPos;
	}
	
protected:
	uchar_t * m_buf;
	uint32_t  m_bufSize;		//缓冲的大小
	uint32_t  m_secSize;        //扇区块大小，一般都是512字节
	uint32_t  m_indexOffset;	//索引
	uint32_t  m_diffOffset;	    //为了节约内存，这里考虑使用小块内存．　所以会用到偏移理
	uint32_t  m_dataPos;        //实际数据领衔
	uint32_t  m_lastWritePos;	//上一次写入数据文件偏移
	bool      m_bOpen;			//是否已经打开

	time_t    m_lastWtTm;		//最后一次写入的时间
	uint32_t  m_delayWtSecs;	//最大延时写入时间

	uint32_t  m_lastFrmTm;	    //最后一个I帧的时候

	uint32_t  m_lastFrmOffset;  //最后一个I帧的偏移
	uint32_t  m_lastFrmLen;		//最后一个I帧的长度

	//方便解析时使用,也就是回放时使用
	int      m_frmIdxCnt;
	int      m_gpsIdxCnt;
	STFS_REC_IDX     * m_pFrmIdx;
	STFS_GPS_IDX     * m_pGPSIdx;
	uchar_t *  m_frameBuf;
	uint32_t   m_frmBufSz;

	uchar_t *  m_gpsBuf;
	uint32_t   m_gpsBufSz;
	CCross_Lock m_lock;
};


#endif /* _REC_FRAME_INDEX_H_ */

#endif // header guard
